﻿#region Copyright
//Copyright 2012 Phelon Aka. Jon H.
/*
    This file is part of BGBuddy - Ultimate PVP Suite.

    BGBuddy - Ultimate PVP Suite is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BGBuddy - Ultimate PVP Suite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Styx.Plugins;
using Styx;
using Styx.Helpers;
using Styx.CommonBot.POI;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.Pathing;

namespace UltimatePvPSuite
{
    public static class PvPBuffs
    {
        private static string BerserkerBuff = @"World\Generic\PVP\Runes\PVP_Rune_Berserker.mdx";
        private static string RestorationBuff = @"World\Generic\PVP\Runes\PVP_Rune_Restoration.mdx";
        private static string Speedbuff = @"World\Generic\PVP\Runes\PVP_Rune_Speed.mdx";

        public static bool Pulse()
        {
            if (GetBGPowerUps())
                return true;
            return false;
        }

        public static bool GetBGPowerUps()
        {
            if (PowerUps != null && UltimatePvPSuite.EnableGetBGPowerUps && PowerUps.Distance < Convert.ToInt32(UltimatePvPSuite.GetBGPowerUpsRange))
            {
                WoWPoint moveTo = StyxWoW.Me.Location;
                moveTo = WoWMathHelper.CalculatePointFrom(StyxWoW.Me.Location, PowerUps.Location, PowerUps.InteractRange - 5f);
                if (Navigator.CanNavigateFully(StyxWoW.Me.Location, moveTo))
                {
                    BotPoi.Current = new BotPoi(PowerUps, PoiType.Hotspot);
                    Navigator.MoveTo(moveTo);
                    if (PowerUps.Distance < PowerUps.InteractRange - 5f)
                    {
                        Logger.Write("Trying to get Buff: " + PowerUps.Name.ToString());
                        PowerUps.Interact();

                    }
                    return true;
                }
            }
            return false;
        }

        private static WoWGameObject PowerUps
        {
            get
            {
                using (StyxWoW.Memory.AcquireFrame())
                {
                    return (from Unit in ObjectManager.GetObjectsOfType<WoWGameObject>(false)
                            orderby Unit.Distance
                            where Unit.Model.Equals(BerserkerBuff) || Unit.Model.Equals(RestorationBuff) || Unit.Model.Equals(Speedbuff)
                            where Unit.Distance < Convert.ToInt32(UltimatePvPSuite.GetBGPowerUpsRange)
                            select Unit).FirstOrDefault();
                }
            }
        }
    }
}
